{%extends 'layout.admin.html'%}
{%block content%}
{% load authfilters %}

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/codemirror@5.65.2/lib/codemirror.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/codemirror@5.65.2/theme/monokai.css">
<script src="https://cdn.jsdelivr.net/npm/codemirror@5.65.2/lib/codemirror.js"></script>
<script src="https://cdn.jsdelivr.net/npm/codemirror@5.65.2/mode/sql/sql.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sql-formatter@15.4.5/dist/sql-formatter.min.js"></script>

<div class="page-header d-print-none">
  <div class="container-xl">
    <div class="row g-2 align-items-center">
      <div class="col">
        <h1 class="page-title">Free Online SQL Formatter & Minifier - Beautify and Compress SQL</h1>
      </div>
      <div class="seo-h1">
        <h2>Format and Minify SQL Queries Online</h2>
        <h2>Key Features of Our SQL Tool
            <ul>
                <li>Beautify SQL: Proper indentation and line breaks for readability.</li>
                <li>Minify SQL: Remove comments and extra whitespace for compact output.</li>
                <li>File Upload Support: Upload .sql files or paste your SQL into the editor.</li>
                <li>Download & Copy: Save or copy results instantly.</li>
                <li>100% Client-Side: Your SQL never leaves your browser.</li>
            </ul>
        </h2>
      </div>
    </div>
  </div>
</div>

<style type="text/css">
.sql-editor {
    border: 1px solid #dee2e6;
    border-radius: 0.375rem;
}
.CodeMirror {
    height: 400px;
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    font-size: 14px;
}
.file-stats {
    background-color: #f8f9fa;
    border: 1px solid #dee2e6;
    border-radius: 0.375rem;
    padding: 1rem;
    margin-bottom: 1rem;
}
.stats-item { display: flex; justify-content: space-between; margin-bottom: 0.5rem; }
.stats-item:last-child { margin-bottom: 0; }
.compression-ratio { color: #198754; font-weight: bold; }
</style>

<div class="page-body">
  <div class="container-xl">
    <div class="card">
      <div class="card-header">
        <h3 class="card-title">SQL Editor</h3>
      </div>
      <div class="card-body">
        <div class="sql-editor">
          <textarea id="input-editor" placeholder="Paste your SQL here..."></textarea>
        </div>
        <div class="mt-3">
          <button id="format-btn" class="btn btn-info me-2">Beautify SQL</button>
          <button id="minify-btn" class="btn btn-success">Minify SQL</button>
        </div>
      </div>
    </div>
  </div>
</div>

<script type='text/javascript'>
$(document).ready(function() {
  var editor = CodeMirror.fromTextArea(document.getElementById('input-editor'), {
    mode: 'text/x-sql',
    theme: 'monokai',
    lineNumbers: true,
    indentUnit: 2,
    tabSize: 2,
    lineWrapping: true
  });

  $('#format-btn').on('click', function() {
    var code = editor.getValue();
    if (!code.trim()) { alert('Please enter some SQL first.'); return; }
    try {
      var formatted = sqlFormatter.format(code);
      editor.setValue(formatted);
    } catch (e) { alert('Formatting failed.'); }
  });

  $('#minify-btn').on('click', function() {
    var code = editor.getValue();
    if (!code.trim()) { alert('Please enter some SQL first.'); return; }
    var minified = minifySQL(code);
    editor.setValue(minified);
  });

  function minifySQL(sql) {
    // Remove single-line comments starting with --
    sql = sql.replace(/--[^\n\r]*/g, '');
    // Remove multi-line comments /* ... */
    sql = sql.replace(/\/\*[\s\S]*?\*\//g, '');
    // Collapse whitespace (keep single spaces)
    sql = sql.replace(/\s+/g, ' ').trim();
    return sql;
  }
});
</script>

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "SoftwareApplication",
  "name": "Free Online SQL Formatter and Minifier",
  "applicationCategory": "DeveloperApplication",
  "operatingSystem": "Any",
  "offers": { "@type": "Offer", "price": "0", "priceCurrency": "USD" },
  "description": "Format and minify SQL queries online for free.",
  "keywords": "sql, formatter, minifier, beautifier, online tool"
}
</script>
{%endblock%}
